/**
 * 
 */
package com.technodo.avatar;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.technodo.enums.PlayerType;
import com.technodo.utils.GlobalPreferences;
import com.technodo.utils.JsonUtils;

/**
 * @author Gaurav
 *
 * Profile of a virtual AI player or a player
 */
public class PlayerProfile {
	private String TAG="PlayerProfile";
	/**
	 * File that the player profile was parsed from
	 */	
	private String file;
	/**
	 * Unique Name of the Player 
	 * Usually marked as email address or virtual email address in case of players
	 */
	private String name;
	/**
	 * Title
	 */
	private String title;
	/**
	 * Is the player male?
	 */
	private boolean isMale;
	/**
	 * Icon of the player 
	 */
	private String iconPath;
	/**
	 * Network of the player
	 */
	private String network;
	/**
	 * Type of Player
	 */
	private PlayerType playerType;
	/**
	 * Games played
	 */
	private Integer gamesPlayed;
	/**
	 * Games won
	 */
	private Integer gamesWon;

	/**
	 * Parses json and creates a new product object
	 * 
	 * @param jsonProduct json mapping of a particular product
	 */
	public PlayerProfile(String filename) throws IllegalArgumentException, JSONException {

		Log.i(TAG, "Parsing Player Profiles : " + filename);

		String json = JsonUtils.GetJsonFromFile(filename);
		if ((json == null) || (json.length() == 0)) {
			throw new IllegalStateException("Json profile config file blank " + filename);
		}

		// Create a Jobject from the json
		// May throw json exception in case of malformed json
		JSONObject jsonProfile = new JSONObject(json);


		// Parse the Json tech and convert to Java data structure
		file    = filename;
		name    = jsonProfile.optString("name");
		title   = jsonProfile.optString("title");
		isMale  = jsonProfile.optBoolean("male");
		iconPath= jsonProfile.optString("icon", GlobalPreferences.defaultTechIcon);
		network = jsonProfile.optString("network");
		gamesWon = jsonProfile.optInt("won", 0);
		gamesPlayed = jsonProfile.optInt("played", 0);
		playerType = convertStringToType(jsonProfile.optString("type"));

		Log.d(TAG, "Parsed Player : " + toString());
	}

	private PlayerType convertStringToType(String sType) {
		PlayerType pType;
		if(sType.compareTo("human") == 0) 
			pType = PlayerType.HUMAN;
		else if(sType.compareTo("ai") == 0)
			pType = PlayerType.AI;
		else if(sType.compareTo("friend") == 0)
			pType = PlayerType.FRIEND;
		else
			throw new IllegalArgumentException("Profile Type " + "sType" + " is unmappable.");
		return pType;

	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "PlayerProfile [file=" + file + ", name=" + name + ", title="
				+ title + ", isMale=" + isMale + ", iconPath=" + iconPath
				+ ", network=" + network + ", playerType=" + playerType
				+ ", gamesPlayed=" + gamesPlayed + ", gamesWon=" + gamesWon
				+ "]";
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the isMale
	 */
	public boolean isMale() {
		return isMale;
	}

	/**
	 * @param isMale the isMale to set
	 */
	public void setMale(boolean isMale) {
		this.isMale = isMale;
	}

	/**
	 * @return the iconPath
	 */
	public String getIconPath() {
		return iconPath;
	}

	/**
	 * @param iconPath the iconPath to set
	 */
	public void setIconPath(String iconPath) {
		this.iconPath = iconPath;
	}

	/**
	 * @return the network
	 */
	public String getNetwork() {
		return network;
	}

	/**
	 * @param network the network to set
	 */
	public void setNetwork(String network) {
		this.network = network;
	}

	/**
	 * @return the playerType
	 */
	public PlayerType getPlayerType() {
		return playerType;
	}

	/**
	 * @param playerType the playerType to set
	 */
	public void setPlayerType(PlayerType playerType) {
		this.playerType = playerType;
	}



}
